﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Telerik.Web.UI;
using KPIS.GERP.GBMS.APP_UI.Form;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.MODEL;

namespace KPIS.GERP.GBMS.APP_UI.UserControls
{
    public partial class Bgm_Project_Expense_Detail : System.Web.UI.UserControl
    {
        private IProject projectBLL;
        private ComboBLL comboBLL = new ComboBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            string strType = Session["type"].ToString();
            txtUnitQty.Attributes.Add("onkeypress", "return isNumberKey(event)");
            if (strType.Length > 0)
            {
                projectBLL = Project_Factory.GetProjectFactory(strType);
            }

            //if (!IsPostBack)
            //{
            //    LoadData();
            //}
        }

        public void LoadData()
        {
            string[] strArgs = Session["Args"].ToString().Split('|');
            hBgmExpGrpSeq.Value = strArgs[0];
            hBgmExpSeq.Value = strArgs[1];
            hBgmProjExpSeq.Value = strArgs[2];
            hProjID.Value = strArgs[3];
            hBgmSourceSeq.Value = strArgs[4];
            hParentIndex.Value = strArgs[5];

            if (hBgmExpGrpSeq.Value == "3")
            {
                Panel2.Visible = true;
                Panel3.Visible = false;
                LoadDDLResItemSeq();

                //ddlResourceItem.SelectedValue = strArgs[7];
                //txtUnitPrice.Text = strArgs[8];
                //txtUnitQty.Text = strArgs[9];
            }
            else
            {
                Panel2.Visible = false;
                Panel3.Visible = true;

                txtTotalAmount.Text = strArgs[6];
                //txtDescr.Text = strArgs[10];
            }

            if (hBgmProjExpSeq.Value.Length > 0)
            {
                DataTable dtb = projectBLL.GetDataProjectExpenseBySequence(hBgmProjExpSeq.Value);
                txtDescr.Text = dtb.Rows[0]["descr"].ToString();
            }
        }

        protected void LoadDDLResItemSeq()
        {
            ddlResourceItem.DataSource = comboBLL.ListResourceDDL(Convert.ToInt32(hBgmExpSeq.Value));
            ddlResourceItem.DataBind();
            ddlResourceItem.Items.Insert(0, new RadComboBoxItem("-- เลือกรายการครุภัณฑ์ --", ""));

            //ListProjectExpenseResource
            Grid_t9.DataSource = projectBLL.ListProjectExpenseResource(hProjID.Value, hBgmExpSeq.Value, hBgmSourceSeq.Value);
            Grid_t9.DataBind();
        }
        protected void Grid_t9_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int nRowSelected = 0;
                foreach (GridDataItem data in Grid_t9.MasterTableView.GetSelectedItems())
                {
                    projectBLL.DeleteProjectExpense(data.OwnerTableView.DataKeyValues[data.ItemIndex]["bgm_req_exp_seq"].ToString());
                    nRowSelected++;
                }
            }
            Grid_t9.DataSource = projectBLL.ListProjectExpenseResource(hProjID.Value, hBgmExpSeq.Value, hBgmSourceSeq.Value);
            Grid_t9.DataBind();
        }

        protected void btnPopupSave_Click(object sender, EventArgs e)
        {
            try
            {
                //Label ParentTotalAmount = (Label)((RadGrid)Page.Form.FindControl("ALL").FindControl("pnGridExpense")
                //    .FindControl("gridExpense")).Items[int.Parse(strParentIndex)]["total_amount"]
                //    .FindControl("lbTotalAmount");

                //projectBLL.DeleteProjectExpense(lbReq.Text);
                if (Panel2.Visible)
                {
                    if (txtUnitPrice.Text.Length > 0
                        && txtUnitQty.Text.Length > 0
                        && ddlResourceItem.SelectedValue.Length > 0)
                    {
                        string[] strIcmResItemSeq = ddlResourceItem.SelectedValue.Split('-');
                        int unitQty = Convert.ToInt32(txtUnitQty.Text);
                        decimal unitPrice = Convert.ToDecimal(txtUnitPrice.Text);
                        decimal totalAmount = unitQty * unitPrice;

                        projectBLL.UpdateProjectExpense(hBgmProjExpSeq.Value, hProjID.Value
                            , hBgmSourceSeq.Value, hBgmExpSeq.Value, totalAmount.ToString()
                            , txtDescr.Text, int.Parse(strIcmResItemSeq[0]), unitQty, unitPrice
                            , null, null, null, new SsMng().GetUss(SsMng.SsUserType.EmpSeq));

                        ddlResourceItem.SelectedIndex = 0;
                        txtUnitQty.Text = "";
                        txtUnitPrice.Text = "";
                        //ParentTotalAmount.Text = totalAmount.ToString();
                        //btnPopupSave.Attributes.Add("onclick", "return HideTooltipCurrent("
                        //    + ParentTotalAmount.ClientID + ", " + totalAmount.ToString() + ");");
                    }
                    else if (hBgmProjExpSeq.Value.Length > 0 && Grid_t9.Items.Count < 1)
                    {
                        projectBLL.DeleteProjectExpense(hBgmProjExpSeq.Value);
                    }
                    else
                    { }
                    ClosePopup();
                }
                else
                {
                    if (txtTotalAmount.Text.Length > 0)
                    {
                        projectBLL.UpdateProjectExpense(hBgmProjExpSeq.Value, hProjID.Value, hBgmSourceSeq.Value, hBgmExpSeq.Value
                            , txtTotalAmount.Text, txtDescr.Text, null, null, null, null
                            , null, null, new SsMng().GetUss(SsMng.SsUserType.EmpSeq));
                        //txtTotalAmount.Text = "";
                        //ParentTotalAmount.Text = txtTotalAmount.Text;
                        //btnPopupSave.Attributes.Add("onclick", "return HideTooltipCurrent("
                        //    + ParentTotalAmount.ClientID + ", " + txtTotalAmount.Text + ");");
                    }
                    else if (hBgmProjExpSeq.Value.Length > 0)
                    {
                        projectBLL.DeleteProjectExpense(hBgmProjExpSeq.Value);
                    }
                    else
                    { }
                    ClosePopup();
                }
            }
            catch (Exception ex)
            {
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ClosePopup();
        }

        protected void ClosePopup()
        {
            //((BGM_Project_Expense_New)Page).ClosePanelPopup();
            //((BGM_Project_Expense_New)Page).LoadData();
        }

        protected void ddlResourceItem_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            string[] strIcmResItemSeq = ddlResourceItem.SelectedValue.Split('-');
            if (strIcmResItemSeq[1].Length > 0)
            {
                txtUnitPrice.Text = strIcmResItemSeq[1];
                txtUnitPrice.ReadOnly = true;
            }
            else
            {
                txtUnitPrice.Text = "";
                txtUnitPrice.ReadOnly = false;
            }
        }
    }
}